﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GRC_TPE_WebApp.Model;

namespace GRC_TPE_WebApp.Controller
{
    public static class BusinessController
    {

        /// <summary>
        /// Permet d'ajouter une affaire en base
        /// </summary>
        /// <param name="title"></param>
        /// <param name="state"></param>
        /// <param name="description"></param>
        /// <param name="clientId"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        public static string AddBusiness(String title, String state, String description, String clientId, decimal amount)
        {
            try
            {
                string ret = string.Empty;

                Model_GRC_TPEContainer database = new Model_GRC_TPEContainer();

                //Création d'une affaire temporaire, afin d'effectuer l'insertion en base
                Business currentBusiness = new Business();

                //Création d'un client temporaire, afin d'effectuer l'insertion en base
                //Client currentClient = new Client();

                //Transforme la string en paramètre en int, sinon déclenche l'erreur "Erreur lors de l'ajout d'une affaire"
                int RequestclientID = int.Parse(clientId);

                //Recherche du client correspond à l'id en paramètre
                //try
                //{
                //    currentClient = (from client in database.ClientSet
                //                     where client.Id == RequestclientID
                //                     select client).First();
                //}
                //catch (Exception e)
                //{
                //    return "Aucun client trouvé";
                //}

                //Ajout des paramètres a l'affaire courante

                currentBusiness.Title = title;
                currentBusiness.State = state;
                currentBusiness.Description = description;
                currentBusiness.ClientId = RequestclientID;
                currentBusiness.Amount = amount;

                //Ajout en base
                database.AddToBusinessSet(currentBusiness);

                //Commit
                database.SaveChanges();

                ret = "No error";

                return ret;
            }
            catch (Exception ex)
            {
                return "Erreur lors de l'ajout d'une affaire";
            }
        }

        /// <summary>
        /// Méthode récupérant une affaire en base avec son id
        /// </summary>
        /// <param name="idBusiness"></param>
        /// <returns></returns>
        public static string[] GetBusinessFields(int idBusiness)
        {
            try
            {
                string[] ret = null;

                Model_GRC_TPEContainer database = new Model_GRC_TPEContainer();

                /// récupération de l'affaire par son id
                Business existingBusiness = (from bus in database.BusinessSet
                                           where bus.Id == idBusiness
                                         select bus).First();

                if (existingBusiness != null)
                {
                    ret = new string[5];

                    ret[0] = existingBusiness.Title;
                    ret[1] = existingBusiness.State;
                    ret[2] = existingBusiness.Description;
                    ret[3] = existingBusiness.Client.Name;
                    ret[4] = existingBusiness.Amount.ToString();
                }
                else
                {
                    ret = new string[1];
                    ret[0] = String.Format("L'affaire d'id '{0}' n'existe pas.", idBusiness);
                }

                return ret;
            }
            catch (Exception ex)
            {
                string[] ret = new string[1];

                ret[0] = String.Format("Une erreur est survenue lors de la récupération de l'affaire d'id '{0}' en base.", idBusiness);

                return ret;
            }
        }

        /// <summary>
        /// Méthode permettant la modification d'une affaire en base
        /// </summary>
        /// <param name="idBusiness"></param>
        /// <param name="title"></param>
        /// <param name="state"></param>
        /// <param name="description"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        public static string EditBusiness(int idBusiness, string title, string state, string description, decimal amount)
        {
            try
            {
                string ret = string.Empty;

                Model_GRC_TPEContainer database = new Model_GRC_TPEContainer();

                /// récupération de l'affaire par son id
                Business existingBusiness = (from bus in database.BusinessSet
                                             where bus.Id == idBusiness
                                             select bus).First();

                if (existingBusiness != null)
                {
                    existingBusiness.Title = title;
                    existingBusiness.State = state;
                    existingBusiness.Description = description;
                    existingBusiness.Amount = amount;
                }
                else
                {
                    ret = String.Format("L'affaire d'id '{0}' n'existe pas.", idBusiness);
                }

                database.SaveChanges();

                ret = "No error";

                return ret;
            }
            catch (Exception ex)
            {
                return string.Format("Erreur lors de la mise à jour de l'affaire d'id '{0}'.", idBusiness);
            }
        }

        /// <summary>
        /// Suppression d'une affaire
        /// </summary>
        /// <param name="idBusiness"></param>
        /// <returns></returns>
        public static string DeleteBusiness(int idBusiness)
        {
            try
            {
                string ret = string.Empty;

                Model_GRC_TPEContainer database = new Model_GRC_TPEContainer();

                /// récupération de l'affaire par son id
                Business existingBusiness = (from bus in database.BusinessSet
                                           where bus.Id == idBusiness
                                           select bus).First();

                if (existingBusiness != null)
                {
                    database.DeleteObject(existingBusiness);
                    database.SaveChanges();
                }
                else
                {
                    ret = String.Format("L'affaire d'id '{0}' n'existe pas.", idBusiness);
                }

                ret = "No error";

                return ret;
            }
            catch (Exception ex)
            {
                return string.Format("Erreur lors de la suppression de l'affaire d'id '{0}'.", idBusiness);
            }
        }
    }
}